from sqlalchemy import Column, String, Integer, DateTime, ForeignKey, Text, Enum, JSON
from sqlalchemy.orm import relationship
from datetime import datetime
import enum

from app.db.session import Base

class NodeType(str, enum.Enum):
    """节点类型"""
    PERSON = "person"  # 人物
    ORGANIZATION = "organization"  # 组织
    EVENT = "event"  # 事件
    CONCEPT = "concept"  # 概念
    LOCATION = "location"  # 地点
    OTHER = "other"  # 其他

class Node(Base):
    """知识图谱节点"""
    __tablename__ = "nodes"

    id = Column(String(36), primary_key=True, index=True)
    type = Column(Enum(NodeType), nullable=False)
    name = Column(String(255), nullable=False, index=True)
    description = Column(Text)
    extra_data = Column(JSON)  # 改名为 extra_data
    create_time = Column(DateTime, default=datetime.utcnow)
    update_time = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # 关系
    outgoing_relations = relationship("Relation", foreign_keys="[Relation.source_id]", back_populates="source")
    incoming_relations = relationship("Relation", foreign_keys="[Relation.target_id]", back_populates="target")
    properties = relationship("Property", back_populates="node")

class RelationType(str, enum.Enum):
    """关系类型"""
    IS_A = "is_a"  # 是一个
    PART_OF = "part_of"  # 属于
    RELATED_TO = "related_to"  # 相关
    BORN_IN = "born_in"  # 出生于
    DIED_IN = "died_in"  # 死于
    CREATED = "created"  # 创建了
    BELONGS_TO = "belongs_to"  # 属于
    OTHER = "other"  # 其他

class Relation(Base):
    """知识图谱关系"""
    __tablename__ = "relations"

    id = Column(String(36), primary_key=True, index=True)
    type = Column(Enum(RelationType), nullable=False)
    source_id = Column(String(36), ForeignKey("nodes.id"), nullable=False)
    target_id = Column(String(36), ForeignKey("nodes.id"), nullable=False)
    weight = Column(Integer, default=1)  # 关系权重
    extra_data = Column(JSON)  # 改名为 extra_data
    create_time = Column(DateTime, default=datetime.utcnow)
    update_time = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # 关系
    source = relationship("Node", foreign_keys=[source_id], back_populates="outgoing_relations")
    target = relationship("Node", foreign_keys=[target_id], back_populates="incoming_relations")

class PropertyType(str, enum.Enum):
    """属性类型"""
    TEXT = "text"  # 文本
    NUMBER = "number"  # 数字
    DATE = "date"  # 日期
    URL = "url"  # 链接
    OTHER = "other"  # 其他

class Property(Base):
    """节点属性"""
    __tablename__ = "properties"

    id = Column(String(36), primary_key=True, index=True)
    node_id = Column(String(36), ForeignKey("nodes.id"), nullable=False)
    key = Column(String(255), nullable=False)
    value = Column(Text, nullable=False)
    type = Column(Enum(PropertyType), nullable=False)
    create_time = Column(DateTime, default=datetime.utcnow)
    update_time = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # 关系
    node = relationship("Node", back_populates="properties") 